﻿using FluentScheduler;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using NLog;
using OddUserManager.Data.Repositories;
using OddUserManager.Service.IServices;
using OddUserManager.Service.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OddUserManager.Web.Common
{
    public class GrabJCMatchJob : IJob
    {
        static ILogger log = LogManager.GetCurrentClassLogger();
        public void Execute()
        {
            IRealTimeJCMatchService _service = new RealTimeJCMatchService(new RealTimeJCMatchRepository());

            try
            {
                //var msg1 = MsgService.GetInstance().SendMsg($"新比赛[001]等已经开放下注");

                var matchs = _service.GetNoFinishedMatch();
                if (MaxTimeData.MaxTime == null)
                {
                    MaxTimeData.MaxTime = matchs.Max(p => p.MatchTime);
                }
                else
                {
                    var cm = matchs.Where(p => p.MatchTime > MaxTimeData.MaxTime).ToList();
                    if (cm.Count > 0)
                    {
                        MaxTimeData.MaxTime = matchs.Max(p => p.MatchTime);
                        var msg = MsgService.GetInstance().SendMsg($"新比赛[{cm.FirstOrDefault().MatchNumber}]等已经开放下注"); 
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }

        }

    }


    public static class MaxTimeData {
        public static DateTime? MaxTime;
    }
}
